package com.supcon.exchangedata.service.lq;

import com.supcon.exchangedata.pojo.entity.oracle.CtCar;
import com.supcon.exchangedata.pojo.entity.mysql.operation.lq.LqMaintainCarBase;
import com.supcon.exchangedata.repository.master.MasterCtCareRepository;
import com.supcon.exchangedata.repository.slave7.SlaveLqCarInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/21/9:54
 * @Description:
 */
@Service
@Slf4j
public class LqCarService {
    @Autowired
    private MasterCtCareRepository masterCtCareRepository;
    @Resource
    private SlaveLqCarInfo slaveLqCarInfo;

    @Transactional(rollbackFor = Exception.class)
    public void tranRoadCar() {

        masterCtCareRepository.deleteAllByGroupId(5L);

        List<LqMaintainCarBase> all = slaveLqCarInfo.findRoadCar();
        if (!CollectionUtils.isEmpty(all)) {
            all.forEach((e) -> {
                CtCar ctCar = new CtCar();
                //车牌号
                String carLicense = e.getCarNumber();
                if(StringUtils.isEmpty(carLicense)){
                    ctCar.setLicensePlateNum("--");
                }else {
                    ctCar.setLicensePlateNum(carLicense);
                }
                ctCar.setGroupId(5L);
                ctCar.setCompanyId(5L);
                ctCar.setUpdateTime(LocalDateTime.now());
                ctCar.setCarCode(e.getVehicleNumber());
                ctCar.setCarType(e.getCarType());
                masterCtCareRepository.save(ctCar);
                // CtCar byLicensePlateNum = masterCtCareRepository.findByLicensePlateNum(carLicense);

                /*if (byLicensePlateNum != null) {
                    ctCar = byLicensePlateNum;
                }*/

            });
        }


    }


}
